package org.example.jida;

import cn.com.jit.assp.dsign.DSign;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * 3.1.4（文件）Detach签名/（文件）Detach验签名(原型2) ok
 * 将字符串进行加密后返回加密字符串值
 */
public class DetachSignFileTest {
    public static void main(String[] args) {
        DSign signDs = new DSign();
        signDs.initConfig("E:/root/epolicy/epolicyModel/cssconfig/2.properties");
        //String strFileNameIn = "d:/src.txt";
        String strFileNameIn = "d:/todo.pdf";
        String strFileNameOut = "d:/dsrc.txt";
        /*********************************detachSign签名**********************************/
        signDs.detachSign("", strFileNameIn, strFileNameOut);
        InputStream inputStream = null;
        int readNum = 0;
        try {
            inputStream = new FileInputStream(strFileNameOut);
            readNum = inputStream.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (readNum <= 0) {
            System.out.println("错误号为:" + signDs.getErrorCode());
            System.out.println("错误描述:" + signDs.getErrorMessage());
        } else {
            System.out.println("文件签名成功！");
        }



        /********************************* verifyDetachedSig验签名***************************/
        DSign vSignDs = new DSign();
        vSignDs.initConfig("E:/root/epolicy/epolicyModel/cssconfig/2.properties");
        long longReturn = 0;
        String strFileNameDttached = "d:/dsrc.txt";

        longReturn = vSignDs.verifyDetachedSign(strFileNameIn, "".getBytes(), strFileNameDttached);
        if (longReturn == 0) {
            System.out.println("版本号:" + vSignDs.getCertInfo("VS", 3, ""));
            System.out.println("颁发者:" + vSignDs.getCertInfo("VS", 1, ""));
            System.out.println("证书主题:" + vSignDs.getCertInfo("VS", 0, ""));
            System.out.println("返回签名时的摘要算法：" + vSignDs.getRndigestAlgID());
            System.out.println("返回签名原文: " + vSignDs.getData());
            System.out.println("返回原文文件名：" + vSignDs.getFile("e:/aaa/"));
            //签名结果带时间戳
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new DateFormatSymbols(Locale.SIMPLIFIED_CHINESE));
            String signedTimeStr = "";
            if (vSignDs.getTsaTime() != null) {
                signedTimeStr = formatter.format(vSignDs.getTsaTime());
            }
            System.out.println("时间: " + signedTimeStr);
            System.out.println("证书subjectdn: " + vSignDs.getTsaCertInfo("subjectdn"));
            System.out.println("证书issure： " + vSignDs.getTsaCertInfo("issuerdn"));
            System.out.println("证书SN： " + vSignDs.getTsaCertInfo("serialnumber"));

        } else {
            System.out.println("错误号为:" + vSignDs.getErrorCode());
            System.out.println("错误描述:" + vSignDs.getErrorMessage());
        }
    }
}